package view;

import model.Borrower;
import model.LibrarySystem;
import model.Resource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Collection;

public class BorrowPanel extends JPanel {
    private final LibrarySystem system;
    private final MainFrame mainFrame;
    private JComboBox<Borrower> borrowerCombo;
    private JComboBox<Resource> resourceCombo;
    private JButton borrowButton;
    private JLabel statusLabel;

    public BorrowPanel(LibrarySystem system, MainFrame mainFrame) {
        this.system = system;
        this.mainFrame = mainFrame;
        setLayout(new BorderLayout(10, 10));
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        initComponents();
    }

    private void initComponents() {
        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 标题
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        JLabel titleLabel = new JLabel("借阅资源");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(new Color(25, 25, 112));
        mainPanel.add(titleLabel, gbc);

        // 借阅者选择
        gbc.gridy++;
        gbc.gridwidth = 1;
        mainPanel.add(new JLabel("选择借阅者:"), gbc);

        gbc.gridx = 1;
        borrowerCombo = new JComboBox<>();
        borrowerCombo.setRenderer(new BorrowerRenderer());
        borrowerCombo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        mainPanel.add(borrowerCombo, gbc);

        // 资源选择
        gbc.gridx = 0;
        gbc.gridy++;
        mainPanel.add(new JLabel("选择资源:"), gbc);

        gbc.gridx = 1;
        resourceCombo = new JComboBox<>();
        resourceCombo.setRenderer(new ResourceRenderer());
        resourceCombo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        mainPanel.add(resourceCombo, gbc);

        // 按钮区域
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

        borrowButton = new JButton("确认借阅");
        borrowButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        borrowButton.setBackground(new Color(70, 130, 180));
        borrowButton.setForeground(Color.WHITE);
        borrowButton.addActionListener(this::borrowResource);

        JButton toSearchBtn = new JButton("查找资源");
        toSearchBtn.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        toSearchBtn.addActionListener(e -> mainFrame.showPanel("search"));

        buttonPanel.add(borrowButton);
        buttonPanel.add(toSearchBtn);

        mainPanel.add(buttonPanel, gbc);

        // 状态标签
        gbc.gridy++;
        statusLabel = new JLabel(" ", JLabel.CENTER);
        statusLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        mainPanel.add(statusLabel, gbc);

        add(mainPanel, BorderLayout.CENTER);

        // 刷新数据
        refresh();
    }

    private void borrowResource(ActionEvent e) {
        Borrower borrower = (Borrower) borrowerCombo.getSelectedItem();
        Resource resource = (Resource) resourceCombo.getSelectedItem();

        if (borrower == null || resource == null) {
            showStatus("请选择借阅者和资源", Color.RED);
            return;
        }

        if (system.borrowResource(borrower.getId(), resource.getId())) {
            showStatus("借阅成功! 资源ID: " + resource.getId(), Color.BLUE);
            refresh();
        } else {
            showStatus("借阅失败: 资源不可用或已借阅", Color.RED);
        }
    }

    private void showStatus(String message, Color color) {
        statusLabel.setText(message);
        statusLabel.setForeground(color);
    }

    public void refresh() {
        // 更新借阅者列表
        borrowerCombo.removeAllItems();
        for (Borrower borrower : system.getAllBorrowers()) {
            borrowerCombo.addItem(borrower);
        }

        // 更新可借阅资源列表
        resourceCombo.removeAllItems();
        for (Resource resource : system.getAllResources()) {
            if (resource.isAvailable()) {
                resourceCombo.addItem(resource);
            }
        }

        if (borrowerCombo.getItemCount() > 0) {
            borrowerCombo.setSelectedIndex(0);
        }

        if (resourceCombo.getItemCount() > 0) {
            resourceCombo.setSelectedIndex(0);
        }

        showStatus("准备就绪，请选择借阅者和资源", Color.DARK_GRAY);
    }

    // 自定义渲染器
    static class BorrowerRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                      boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Borrower) {
                Borrower b = (Borrower) value;
                setText(String.format("%s - %s (已借: %d)", b.getId(), b.getName(), b.getBorrowedResources().size()));
            }
            return this;
        }
    }

    static class ResourceRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                      boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Resource) {
                Resource r = (Resource) value;
                setText(String.format("%s - %s [%s]", r.getId(), r.getTitle(), r.getClass().getSimpleName()));
            }
            return this;
        }
    }
}